package com.modular.extra.fm.site.action;

import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.modular.extra.fm.FactoryFmOpt;
import com.modular.extra.fm.om.bean.FmOmUserDo;
import com.modular.extra.fm.site.bean.FmSiteInfoDo;
import com.tool.ToolRedisson;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.tool.abnormal.ComAbnormal;

public class FmSiteSyncPosAction extends ActionAbs {

    @Override
    public ActionOutBo handle() {
        String mn = this.actionInBo.getDataCode();

        // 获取基站负责人手机号
        FmSiteInfoDo fmSiteInfoDo = FactoryFmOpt.FM_SITE_OPT.query().eq("mn", mn).getBean();

        String omDeptId = fmSiteInfoDo.getOmDeptId();

        FmOmUserDo fmOmUserDo = FactoryFmOpt.FM_OM_USER_OPT.query().eq("is_leader", "1").eq("om_dept_id", omDeptId).getBean();

        if (ObjectUtil.isNull(fmOmUserDo)) {
            throw new ComAbnormal("找不到部门负责人...");
        }

        String phone = fmOmUserDo.getPhone();

        String code = Opt.ofBlankAble(this.actionInBo.getParamStr("code")).orElseThrow(() -> new ComAbnormal("请确认校验码正确输入!"));

        if (!StrUtil.equals(ToolRedisson.me.get("captcha:" + phone), code)) {
            throw new ComAbnormal("验证码错误，请确认后重试...");
        }

        String lat = Opt.ofBlankAble(this.actionInBo.getParamStr("lat")).orElseThrow(() -> new ComAbnormal("请确认坐标信息正确输入!"));
        String lng = Opt.ofBlankAble(this.actionInBo.getParamStr("lng")).orElseThrow(() -> new ComAbnormal("请确认坐标信息正确输入!"));

        fmSiteInfoDo.setLatitude(lat);
        fmSiteInfoDo.setLongitude(lng);

        FactoryFmOpt.FM_SITE_OPT.updatePos(fmSiteInfoDo);

        return new ActionOutBo("ok");
    }

    @Override
    public ActionInfo info() {
        return new ActionInfo("更新对应基站的坐标信息...", "fm-site-sync-pos-info", this);
    }
}
